/*
 * SaveExperiment.java
 * 
 * Created on Mar 29, 2009, 1:19:13 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.stock.save;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import portfolio.configMessage.configMessageEvent;
import portfolio.configMessage.configMessageFrame;
import portfolio.configMessage.configMessageListener;
import portfolio.dbHost.ProjectTreeTab;
import portfolio.dbHost.project.Project;
import portfolio.dbHost.project.attributes.StockExperiments;
import portfolio.mainScreen;
import portfolio.stock.ConfigureSVMPanel;
import portfolio.stock.analize.AnalizeArguments;
/**
 *
 * @author ACA
 */
    /***********************************************************/
    public class SaveExperiment implements configMessageListener, ActionListener, Serializable 
    {
        private configMessageFrame msgFrame  = null;
        
        private mainScreen         fatherFrame        = null;
        private Vector             vSelectedVariables = null;
        private String             sStockName         = null;
        private AnalizeArguments   analizeArguments   = null;
        
        private ConfigureSVMPanel selectRef;
        /*******************************************/
        public void actionPerformed(ActionEvent e) 
        {
            try {
                if (msgFrame == null) {
                    msgFrame = new configMessageFrame (fatherFrame, "Experiment Name.", 250, 120);
                    msgFrame.showMessageFrame();
                    msgFrame.addConfigMessageListener(this);
                }
                return;
            //} catch (SQLException ex) {
            //    fatherFrame.displayMessage("SaveExperiment.actionPerformed 148", ex.getMessage());
            } catch (IOException ex) {
                fatherFrame.displayMessage("SaveExperiment.actionPerformed 150", ex.getMessage());
            } catch (Exception exp) {
                fatherFrame.displayMessage("SaveExperiment.actionPerformed 105", exp.getMessage());
            }
        }
        /**********************************/

        public void okSelected(configMessageEvent e) {
            try {
                Project project = fatherFrame.getDBLoggedHost().getProject();
                
                analizeArguments = new AnalizeArguments ();
                analizeArguments.preprocess      = selectRef.getPreprocessOK().isSelected();
                analizeArguments.preprocess_type = selectRef.getPreprocessGroup();
                analizeArguments.components      = selectRef.getComponentsConfig ();
                analizeArguments.iterations      = selectRef.getIterationsConfig ();

                analizeArguments.svm_type    = selectRef.getTypeGroup    ();
                analizeArguments.kernel_type = selectRef.getKernelGroup  ();

                analizeArguments.degree      = selectRef.getDegreeConfig ();
                analizeArguments.gamma       = selectRef.getGammaConfig  ();
                analizeArguments.coef0       = selectRef.getCoef0Config  ();
                analizeArguments.C           = selectRef.getCostConfig   ();
                analizeArguments.nu          = selectRef.getNuConfig     ();
                analizeArguments.p           = selectRef.getElossConfig  ();
                analizeArguments.eps         = selectRef.getETolConfig   ();
                analizeArguments.shrinking   = selectRef.getShrinking    ();

                ByteArrayOutputStream blobSelectVariables  = this.processBlob(vSelectedVariables);
                ByteArrayOutputStream blobAnalizeArguments = this.processBlob(analizeArguments);

                String sExperimentName = sStockName + "." + msgFrame.getFieldName();
                int iExperimentNameLen = sExperimentName.length();
                if (iExperimentNameLen >= StockExperiments.EXPERIMENTNAME_LEN) {
                    fatherFrame.displayMessage("SaveExperiment.okSelected 77", "Warning: Experiment name truncated to the maximun lenght allowed of" + StockExperiments.EXPERIMENTNAME_LEN);
                    sExperimentName = sExperimentName.substring(0, StockExperiments.EXPERIMENTNAME_LEN -1);
                }

                Vector vValues = new Vector ();
                vValues.add (sExperimentName);
                vValues.add(blobSelectVariables );
                vValues.add(blobAnalizeArguments);
System.out.println ("----------------------------> break 3.4");
                project.getStockExperiments().insert(vValues);

                msgFrame.moveToFront();                

                project.updateProjectTree();
                fatherFrame.repaintStockTabedPanels (); //NOT original
            } catch (Exception ex) {
                fatherFrame.displayMessage("SaveExperiment.okSelected 148", ex.getMessage());
            } 
            msgFrame.dispose();
        }

        public void cancelSelected(configMessageEvent e) {
            msgFrame.dispose();
        }
        /**
         * 
         */
        public SaveExperiment (mainScreen        fatherFrameParam,
                               JButton           jSaveButtonParam,
                               ConfigureSVMPanel mainFrameParam,
                               String            sStockNameParam,
                               Vector            vSelectedVariablesParam)  throws Exception 
        {
            fatherFrame = fatherFrameParam;
            selectRef    = mainFrameParam;
            sStockName   = sStockNameParam;
            vSelectedVariables = vSelectedVariablesParam;
            jSaveButtonParam.addActionListener(this);
            
            return;
        }
        /**
         * 
         */
        public void ResetSaveExperiment (String  sStockNameParam,
                                         Vector  vSelectedVariablesParam)
        {
            sStockName   = sStockNameParam;
            vSelectedVariables = vSelectedVariablesParam;
            
            return;
        }
        /**
         * 
         */
        private ByteArrayOutputStream processBlob (Object inputObject) throws IOException 
        {
                ByteArrayOutputStream blobObject = new ByteArrayOutputStream ();

                ObjectOutputStream outputStream = new ObjectOutputStream (blobObject);
                outputStream.writeObject(inputObject);
                outputStream.close ();
                
                return blobObject;        
        }
        /**
         * 
         */
        public configMessageFrame getMessageFrame ()
        {
            return msgFrame;
        }
    } /* End Class submitButton::selectIndexes::cancelButton */
